/*
 * Copyright 2007-2008 Michele Mostarda ( michele.mostarda@gmail.com ).
 * All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package org.asemantics.anthill.chatconsole;

import org.apache.tools.ant.Project;
import org.asemantics.anthill.CommandListenerTask;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;

/**
 * Implements the listener of a chat waiting for a commmand.
 */
public class ChatCommandListener implements MessageListener {

    /**
     * The command listener task who registered this listener.
     */
    private CommandListenerTask commandListenerTask;

    /**
     * The associated command line.
     */
    private ChatCommandLine chatCommandLine;

    /**
     * The chat console.
     */
    private ChatConsoleOutputStream chatConsole;

    /**
     * Constructor.
     *
     * @param ccl the command line to expose.
     * @param cc the chat console to use.
     */
    public ChatCommandListener(CommandListenerTask clt, ChatCommandLine ccl, ChatConsoleOutputStream cc) {
        if(clt == null) {
            throw new IllegalArgumentException();
        }
        if(ccl == null) {
            throw new IllegalArgumentException();
        }
        if(cc == null) {
            throw new IllegalArgumentException();
        }
        commandListenerTask = clt;
        chatCommandLine     = ccl;
        chatConsole         = cc;
    }

    /**
     * Constructor.
     * 
     * @param ccl
     */
    public ChatCommandListener(CommandListenerTask clt, ChatCommandLine ccl) {
        this(clt, ccl, new ChatConsoleOutputStream() );
    }

    public void processMessage(Chat chat, Message message) {
        commandListenerTask.log("chat command listener received message: " + message.getBody(), Project.MSG_DEBUG);

        try {
            chatConsole.setChat(chat);
            chatCommandLine.processChatEntry(commandListenerTask, chatConsole, message.getBody());
            chatConsole.flush();
        } catch (XMPPException xmppe) {
            try {
                chat.sendMessage("An error occurred with XMPP protocol: " + xmppe.getMessage());
            } catch (XMPPException e) { e.printStackTrace(); }
        } catch(Exception e) {
            try {
                chat.sendMessage("An error occurred while processing command: " + e.getMessage());
            } catch (XMPPException e1) { e1.printStackTrace(); }
        }
    }
}
